<?php
namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Db;
use think\Request;

class Index extends HomeBase
{
    public function index()
    {
		//header("Access-Control-Allow-Origin: *");
        $params = Request::instance()->param();
        if(!isset($params['sign'])  && !isset($params['timestamp']) ){
            return json_encode(['code'=>'-1','msg'=>'error','list'=>'签名错误']);
        }
        $sign = $params['sign'];
        $timestamp = $params['timestamp'];
        unset($params['sign']);
        unset($params['timestamp']);
        unset($params['avatar']);
        //秘钥
        $secret = '1dFlxLhiuLqnUZe9kA';

        ksort($params,0);
        $str = '';

        //参数签名
        foreach($params as $key=>$param){
            $str .= $key.$param;
        }
		
        //签名获得
        $service_api = md5($secret.$str.$timestamp.$secret);
        //对比签名
        if($service_api != $sign){
            return json(['code'=>'-1','msg'=>'error','list'=>'签名错误']);
        }

        $articleid   = Request::instance()->param('articleid');
        $is_pay = Request::instance()->param('is_pay');

        if($is_pay == 'pay'){

            $responce = Db::table('lsj_article')
                ->where('id',$articleid)
                ->field("title,create_time,pay_content as content,reading,pay_photos")
                ->find();

			if($responce){
                $responce['content'] = htmlspecialchars_decode($responce['content']);
                return json([
                    'code' => 0,
                    'message' => 'success',
                    'list' => $responce
                ]);

			}else{
				return json([
					'code' => -10000,
					'message' => 'error',
					'list' => "没有数据了"
				]);
			}
        }else{

            $responce = Db::table('lsj_article')
                ->where('id',$articleid)
                ->field('title,create_time,content,reading')
                ->find();
//            $buy_num = Db::table('lsj_order')
//                ->where([
//                    'article_id' => $articleid,
//                    'paystatus' => 1
//                ])
//                ->count();
            return $this->fetch('index',['responces' => $responce]);
        }

    }
}